package com.chen.qmzy.service.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BeanUtils {

    public static Object copyProperties(Object source, Object target, String... ignoreProperties) {
        if (source == null) {
            return target;
        }
        BeanUtil.copyProperties(source, target, ignoreProperties);
        return target;
    }

    public static <T> List<T> copyList(List sources, Class<T> clazz) {
        return copyList(sources, clazz, null);
    }

    public static <T> List<T> copyList(List sources, Class<T> clazz, Callback<T> callback) {
        List<T> targetList = new ArrayList<>();
        if (sources != null) {
            try {
                for (Object source : sources) {
                    T target = clazz.newInstance();
                    copyProperties(source, target);
                    if (callback != null) {
                        callback.set(source, target);
                    }
                    targetList.add(target);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetList;
    }


    public static interface Callback<T> {
        void set(Object source, T target);
    }

    //检查Pojo对象是否有null字段
    public static boolean checkPojoNullField(Object o, Class<?> clz) {
        try {
            Field[] fields = clz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.get(o) == null) {
                    return false;
                }
            }
            if (clz.getSuperclass() != Object.class) {
                return checkPojoNullField(o, clz.getSuperclass());
            }
            return true;
        } catch (IllegalAccessException e) {
            return false;
        }
    }


    public static <T> T mapToBean(Map<String, Object> source, Class<T> destBeanClass) {
        return BeanUtil.mapToBean(source, destBeanClass, false, new CopyOptions());
    }

    public static <T> Map<String, Object> beanToMap(T sourceBean) {
        return BeanUtil.beanToMap(sourceBean);
    }

    /**
     * 将List<Map<String,Object>>转换为List<Bean>
     *
     * @param sourceList
     * @param destBeanClass
     * @return
     */
    public static <T> List<T> listsToBeans(List<Map<String, Object>> sourceList, Class<T> destBeanClass) {
        List<T> beanList = new ArrayList<>();
        for (Map<String, Object> map : sourceList) {
            T dest = mapToBean(map, destBeanClass);
            beanList.add(dest);

        }
        return beanList;
    }

    /**
     * 将List<Bean>转换为List<Map<String, Object>>
     *
     * @param sourceList
     * @return
     */
    public static <T> List<Map<String, Object>> beansToList(List<T> sourceList) {
        List<Map<String, Object>> destList = new ArrayList<>();
        for (Object source : sourceList) {
            Map<String, Object> map = BeanUtil.beanToMap(source);
            destList.add(map);
        }
        return destList;
    }
}
